package com.hq.petmap.task;

import android.os.AsyncTask;
import android.util.Log;

public abstract class HQTask extends AsyncTask<HQTaskParams, Object, HQTaskResult>{
	
	private static final String TAG = "HQTask";
	private HQTaskAdapter taskAdapter = null;

	public HQTaskAdapter getTaskAdapter() {
		return taskAdapter;
	}

	public void setTaskAdapter(HQTaskAdapter taskAdapter) {
		this.taskAdapter = taskAdapter;
	}
	
	abstract protected HQTaskResult run(HQTaskParams... params);

	@Override
	protected HQTaskResult doInBackground(HQTaskParams... params) {
		HQTaskResult result = run(params);
		return result;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		if (taskAdapter != null) {
			taskAdapter.onPreExecute(this);
		}
		Log.d(TAG, " onPreExecute.");
	}

	@Override
	protected void onPostExecute(HQTaskResult result) {
		super.onPostExecute(result);
		if (taskAdapter != null) {
			taskAdapter.onPostExecute(this, result);
		}
		Log.d(TAG, " onPostExecute.");
	}

	@Override
	protected void onProgressUpdate(Object... values) {
		super.onProgressUpdate(values);
		if (taskAdapter != null) {
			if (values != null && values.length > 0) {
				taskAdapter.onProgressUpdate(this, values[0]);
			}
		}
		Log.d(TAG, " onProgressUpdate.");
	}


	@Override
	protected void onCancelled() {
		super.onCancelled();
		if (taskAdapter != null) {
			taskAdapter.onCancelled(this);
		}
		Log.d(TAG, " onCancelled.");
	}

}
